/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.businesslogic;

import cn.ekuma.epos.bean.util.sale.TicketLineHelper;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.data.basic.BasicException;
import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.Location;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductPrefPrice;
import com.openbravo.bean.ProductPrice;
import com.openbravo.bean.ProductScore;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.bean.sales.TicketLine;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class TicketUtil {
    /**
     * 修改会员时调用此，用于重新设置单据商品价格基础为会员价，然后重新计算整单价,当当前商品是特价商品时除外
     * @param mTicket
     * @param mCustomer
     */
     public static void changeCustomer(I_DataLogicSales dlSales,Ticket mTicket,BusinessPartner mCustomer,String priceGroup) throws BasicException{
            BusinessPartner befCustomer=mTicket.getCustomer();
            if((befCustomer!=null&&mCustomer!=null)||(befCustomer==null&&mCustomer==null))
                return;
            List<TicketLine> mTicketLines=mTicket.getLines();
            String p_id;
            for(TicketLine line:mTicketLines){
                p_id=line.getProductID();
                if(!line.isIsPrefSell()&&p_id!=null&&!p_id.isEmpty()){
                    Product oProduct = dlSales.getProductInfo(p_id);
                    if (oProduct != null) {
                    	double salePrice=getProductPrice(dlSales,mCustomer,oProduct,priceGroup);
                        TicketLineHelper.setPrice(line,salePrice);
                    }
                }
            }
     }

     public static double getProductPrice(I_DataLogicSales dlSales,BusinessPartner mCustomer,Product oProduct,String priceGroup)throws BasicException{
    	 double salePrice=oProduct.getPriceSell();
         if(priceGroup!=null){
 				ProductPrice groupPrice=dlSales.findProductPriceByGroupAndProduct(priceGroup, oProduct.getID());
 			    if(groupPrice!=null){
 			    	if (mCustomer != null && groupPrice.getM_dCustomerPrice() > 0) {
 			    		salePrice = groupPrice.getM_dCustomerPrice();
 					}else
 						salePrice=groupPrice.getM_dPriceSell();
 			    }
 		 }else  if(mCustomer!=null&&(oProduct.getM_dCustomerPrice()>0))
 			salePrice=oProduct.getM_dCustomerPrice();
         return salePrice;
     }
     
     
     /**
      * 检查指定行是否是商品行，如包含则跟据特价规则重设相应的价格。
      * @param mTicketLine
      */
     public static TicketLine checkTicketLineProductPrefPrice(I_DataLogicSales dlSales,Ticket mTicket,TicketLine mTicketLine,Location location) throws BasicException{
    	 String m_id=mTicketLine.getProductID();
    	 if(mTicket.getTicketType()!=Ticket.RECEIPT_NORMAL||!location.isPrefSell()||m_id==null)
        	 return null;
         if(m_id!=null&&!m_id.isEmpty()){
                ProductPrefPrice prefPrice = dlSales.getProductPrefPriceByProductID(m_id);
                if(prefPrice!=null){
                	mTicketLine.setPrice(mTicketLine.getDefaultPrice());
                	mTicketLine.setPrefSell(false);
                    String prefType=prefPrice.getPrefType();
                    boolean isPrefProduct=false;
                    if(ProductPrefPrice.PREFTYPE_OverPref.equalsIgnoreCase(prefType)){
                        double num=mTicketLine.getMultiply();
                        double minNum=prefPrice.getMinNum();
                        isPrefProduct=num>=minNum;
                    }else if(ProductPrefPrice.PREFTYPE_QuotaPref.equalsIgnoreCase(prefType)){
                        double quotaNum=prefPrice.getStoreNum();
                        double selledNum=prefPrice.getSelledNum();
                        isPrefProduct=(quotaNum-selledNum)>=mTicketLine.getMultiply();
                    }else if(ProductPrefPrice.PREFTYPE_TimeAndOverPref.equalsIgnoreCase(prefType)){
                        double num=mTicketLine.getMultiply();
                        double minNum=prefPrice.getMinNum();
                        Date start=prefPrice.getM_dStartDate();
                        Date stop=prefPrice.getM_dStopDate();
                        Date now=new Date();
                        boolean isbefStop=stop==null?true:(stop.compareTo(now)>=0);
                        isPrefProduct=(num>=minNum)&&(start.compareTo(now)<=0)&&isbefStop;
                    }else if(ProductPrefPrice.PREFTYPE_TimeAndQuotaPref.equalsIgnoreCase(prefType)){
                        double quotaNum=prefPrice.getStoreNum();
                        double selledNum=prefPrice.getSelledNum();
                        Date start=prefPrice.getM_dStartDate();
                        Date stop=prefPrice.getM_dStopDate();
                        Date now=new Date();
                        boolean isbefStop=stop==null?true:(stop.compareTo(now)>=0);
                        isPrefProduct=((quotaNum-selledNum)>=mTicketLine.getMultiply())&&(start.compareTo(now)<=0)&&isbefStop;
                    }else if(ProductPrefPrice.PREFTYPE_TimePref.equalsIgnoreCase(prefType)){
                        Date start=prefPrice.getM_dStartDate();
                        Date stop=prefPrice.getM_dStopDate();
                        Date now=new Date();
                        boolean isbefStop=stop==null?true:(stop.compareTo(now)>=0);
                        isPrefProduct=(start.compareTo(now)<=0)&&isbefStop;
                    }
                    if(isPrefProduct){
                       String prefModeType=prefPrice.getPrefModeType();
                       mTicketLine.setIsPrefSell(true);
                       mTicketLine.setPrefSellID(prefPrice.getId());
                       mTicketLine.setConsultCost(prefPrice.getPrefBuyPrice());
                       if(ProductPrefPrice.PREFMODETYPE_Rate.equalsIgnoreCase(prefModeType)){
                           mTicketLine.setPriceRate(prefPrice.getPrefRate());
                       }else if(ProductPrefPrice.PREFMODETYPE_SellPrice.equalsIgnoreCase(prefModeType)){
                           TicketLineHelper.setPrice(mTicketLine,prefPrice.getSellPrice());
                       }else if(ProductPrefPrice.PREFMODETYPE_Largess.equalsIgnoreCase(prefModeType)){
                           Product oProduct = dlSales.getProductInfo(prefPrice.getM_sLargessId());
                           if (oProduct != null) {
                                TicketLine largessLine=new TicketLine(oProduct, prefPrice.getLargessNum(), 0.0, null, (java.util.HashMap) (oProduct.getProperties().clone()), mTicket.getM_dPriceRate());
                                //largessLine.setIsPrefSell(true);

                                TicketLineHelper.setDefaultPrice(largessLine,oProduct.getPriceSell());
                                return largessLine;
                           }
                       }else if(ProductPrefPrice.PREFMODETYPE_ImmeLargess.equalsIgnoreCase(prefModeType)){

                           TicketLineHelper.setPrice(mTicketLine,0.0);
                       }
                    }
                }
         }
         return null;
     }

     public static void checkTicketLineProductScore(I_DataLogicSales dlSales,Ticket mTicket,TicketLine mTicketLine,int scoreType,Location location) throws BasicException{
         if(mTicket.getCustomer()==null||!location.isScore())
             return;
         String m_id=mTicketLine.getProductID();
         if(m_id!=null&&!m_id.isEmpty()){
        	 ProductScore productScore=dlSales.getProductScoreByProductAndType(m_id, scoreType);
        	 if(productScore!=null)
        		 mTicketLine.setScore(productScore.getScore());
        	 else
        		 mTicketLine.setScore(mTicketLine.getPrice());
         }
     }
     
     
     public static TicketLine insertLine(I_DataLogicSales dlSales,Ticket mTicket,int order,TicketLine mTicketLine,Location location)throws BasicException{
         mTicket.insertLine(order, mTicketLine);
         TicketLine addLine=null;
         if(Ticket.RECEIPT_REFUND==mTicket.getTicketType())
                      mTicketLine.setMultiply(-mTicketLine.getMultiply());
         else{
        	 addLine=checkTicketLineProductPrefPrice(dlSales, mTicket, mTicketLine,location);
             if(addLine!=null)
                mTicket.insertLine(order+1, addLine);
         }
         return addLine;
     }

     public static TicketLine addLine(I_DataLogicSales dlSales,Ticket mTicket,TicketLine mTicketLine,Location location)throws BasicException{
         TicketLine addLine=null;
         mTicket.addLine(mTicketLine);
         if(Ticket.RECEIPT_REFUND==mTicket.getTicketType())
                  mTicketLine.setMultiply(-mTicketLine.getMultiply());
         else {
        	 addLine=checkTicketLineProductPrefPrice(dlSales, mTicket, mTicketLine,location);
        	 if(addLine!=null){
               mTicket.addLine(addLine); 
           }
         }  
         return addLine;
     }

     public static void changeRate(Ticket mTicket,double rate){
         double prevRate=mTicket.getM_dPriceRate();
         for(TicketLine line:mTicket.getLines()){
             if(line.isIsPrefSell()||prevRate!=line.getPriceRate())
                 continue;
             line.setPriceRate(rate);
         }
         mTicket.setM_dPriceRate(rate);
     }
}
